rm(list = ls())
#install.packages("plotly")
library(haven)
library(tidyverse)
library(ggplot2)
library(readxl)
library(plotly)

1 Importation des données

path <- here::here()
InGrDis <- read.csv(paste0(path, "/annual-growth-in-gni-per-capita.csv"))
GenIngInd <- read.csv(paste0(path, "/gender-inequality-index.csv"))
PopGrAn <- read.csv(paste0(path, "/population-growth-annual.csv"))

2 Graphique I

#Filter pour le Niger 
InGrDis_niger <- InGrDis %>% dplyr::filter(Region.Alpha.3.Code=="NER")
InGrDis_niger <- InGrDis_niger %>% select("Region.Name", "Start.Year", "Value")
#Afrique de l'ouest

##### Aggrégation #####  

#Pour l'afrique de l'ouest
western_africa_1 <- c("Benin", "Burkina Faso", "Cabo Verde", "Côte d’Ivoire", "Gambia", "Ghana", "Guinea", "Guinea-Bissau", "Liberia","Mali","Mauritania", "Niger", "Nigeria", "Senegal", "Togo","Sierra Leone")

#Créer une variable pour recenser les pays d'Afrique de l'Ouest
InGrDis$western_africa <- ifelse(InGrDis$Region.Name %in% western_africa_1, "western Africa","world")

InGrDis_ouest <- InGrDis %>%
  filter(western_africa == "western Africa")

InGrDis_ouest <- InGrDis_ouest %>%
  group_by(Start.Year) %>%
  summarise(Value = mean(Value))

InGrDis_ouest$Region.Name <- "western Africa"
InGrDis_ouest <- InGrDis_ouest %>%
  select(Region.Name, Start.Year, Value)

###### Pour le monde #####
InGrDis_world <- InGrDis %>%
  group_by(Start.Year) %>%
  summarise(Value = mean(Value))
InGrDis_world$Region.Name <- "world"

InGrDis_world <- InGrDis_world %>%
  select(Region.Name, Start.Year, Value)

##### Merger en superposant ###### 
InGrDisFinal <- rbind(rbind(InGrDis_world, InGrDis_ouest), InGrDis_niger)


###### Graphe #######
ggplot(InGrDisFinal) +
  aes(x = Start.Year, y = Value, colour = Region.Name) +
  # ajouter Les points
  geom_point(shape = "circle", size = 2) + 
  #Ajouter la courbe reliant les points
  geom_path(size = 0.5, show.legend = NULL) +  
  scale_fill_hue(direction = 1) +
  #Couleurs des différentes courbes
  scale_color_manual(
  values = c(Niger = "#85C1E9",
  `western Africa` = "#5B5B5C",
  world = "#B2BABB")
  )+
  # Titre, titre des axes, sources...
  labs(title = "Annual population growth (%)",
       caption = "Source : World Bank", x=NULL, y=NULL, color = NULL) +
  # Echelles des axes en abscisses
  scale_x_continuous(breaks=seq(from=1960, to = 2020, by=10))+
  # Echelles des axes en ordonnées
  scale_y_continuous(breaks = seq(from=-7, to = 10, by=1.5), expand = c(0,0))+ #l'echelle des axes
  ylim(-7,10)+
  # Thèmes du fond
  theme_minimal()+
  theme(
    # Paramètre du titre, couleur, police, position
    plot.title = element_text(colour = "#0F4761", face = "italic", size=9, vjust = 8),
    # Paramètre de la legende, taille...
    legend.text = element_text(size = 8), 
    # Position de la legende
    legend.position = c(0.132, 1.03),
    legend.direction = "horizontal", 
    # Paramètres pour la source, position...
    plot.caption = element_text(hjust = 0),
    # Effacer les petites lignes du cadran
    panel.grid.minor = element_blank(),
    # Mettre les lignes horizontales en forme de tirets
    panel.grid.major.y = element_line(linetype = "dashed"),
    # Marge pour le cadran
    plot.margin = margin(30,30,30,30))

3 Graphique II

#Filter pour le Niger 
PopGrAn_Niger <- PopGrAn %>% dplyr::filter(Region.Alpha.3.Code=="NER")
PopGrAn_Niger <- PopGrAn_Niger %>%
  select(Region.Name, Start.Year, Value)
#Afrique de l'ouest

##### Aggrégation #####  

#Créer une variable pour recenser les pays d'Afrique de l'Ouest
PopGrAn$western_africa <- ifelse(PopGrAn$Region.Name %in% western_africa_1, "western Africa","world")

PopGrAn_ouest <- PopGrAn %>%
  filter(western_africa == "western Africa")

PopGrAn_ouest <- PopGrAn_ouest %>%
  group_by(Start.Year) %>%
  summarise(Value = mean(Value))

PopGrAn_ouest$Region.Name <- "western Africa"
PopGrAn_ouest <- PopGrAn_ouest %>%
  select(Region.Name, Start.Year, Value)

###### Pour le monde #####
PopGrAn_world <- PopGrAn %>%
  group_by(Start.Year) %>%
  summarise(Value = mean(Value))
PopGrAn_world$Region.Name <- "world"

PopGrAn_world <- PopGrAn_world %>%
  select(Region.Name, Start.Year, Value)

##### Merger en superposant ###### 
PopGrAnFinal <- rbind(rbind(PopGrAn_world, PopGrAn_ouest), PopGrAn_Niger)

###### Graphe #######
ggplot(PopGrAnFinal) +
  aes(x = Start.Year, y = Value, colour = Region.Name)+
  geom_point(shape = "circle", size = 2) +
  scale_color_hue(direction = 1) +
  geom_line(size = 0.5, show.legend = NULL) +  
  scale_color_manual(
  values = c(Niger = "#85C1E9",
  `western Africa` = "#5B5B5C",
  world = "#B2BABB")
  )+
  labs(title = "Annual population growth (%)",
       caption = "Source : World Bank", x=NULL, y=NULL, color = NULL) +
  geom_abline(intercept = 0, slope = 0, color="white")+
  scale_x_continuous(breaks=seq(from=1960, to = 2020, by=10))+
  scale_y_continuous(breaks = seq(0, 4, by = 1),position = "left", labels = c("", 1, 2, 3, 4), expand = c(0,0))+ 
  coord_cartesian(ylim = c(0, 4))+
  theme_minimal()+
  theme(
    plot.title = element_text(colour = "#0F4761", face = "italic", size=9, vjust = 8),
    legend.text = element_text(size = 8), 
    legend.position = c(0.132, 1.03), 
    legend.direction = "horizontal", 
    plot.caption = element_text(hjust = 0),
    panel.grid.minor = element_blank(),
    panel.grid.major.y = element_line(linetype = "dashed"),
    plot.margin = margin(30,30,30,30))

4 Graphique III

#Filter pour le Niger 
GenIngInd_niger <- GenIngInd %>% dplyr::filter(Region.Alpha.3.Code=="NER")
GenIngInd_niger <- GenIngInd_niger %>% select("Region.Name", "Start.Year", "Value")
#Afrique de l'ouest

##### Aggrégation #####  

#Pour l'afrique de l'ouest

#Créer une variable pour recenser les pays d'Afrique de l'Ouest
GenIngInd$western_africa <- ifelse(GenIngInd$Region.Name %in% western_africa_1, "western Africa","world")

GenIngInd_ouest <- GenIngInd %>%
  filter(western_africa == "western Africa")

GenIngInd_ouest <- GenIngInd_ouest %>%
  group_by(Start.Year) %>%
  summarise(Value = mean(Value))

GenIngInd_ouest$Region.Name <- "western Africa"
GenIngInd_ouest <- GenIngInd_ouest %>%
  select(Region.Name, Start.Year, Value)

###### Pour le monde #####
GenIngInd_world <- GenIngInd %>%
  group_by(Start.Year) %>%
  summarise(Value = mean(Value))
GenIngInd_world$Region.Name <- "world"

GenIngInd_world <- GenIngInd_world %>%
  select(Region.Name, Start.Year, Value)

##### Merger en superposant ###### 
GenIngIndFinal <- rbind(rbind(GenIngInd_world, GenIngInd_ouest), GenIngInd_niger)


###### Graphe #######
p <-ggplot(GenIngIndFinal) +
      aes(x = Start.Year, y = Value, fill=Region.Name, colour = Region.Name) +
      geom_point(aes(text = paste(Region.Name, ": ",
                        round(Value,1))),shape = "bullet", size=2) +
      geom_line(size=1) +  
      scale_fill_hue(direction = 1) +  
      scale_color_manual(
      values = c(Niger = "#85C1E9",
      `western Africa` = "#5B5B5C",
      world = "#B2BABB")
      )+
      scale_fill_manual(
      values = c(Niger = "#85C1E9",
      `western Africa` = "#5B5B5C",
      world = "#B2BABB")
      )+
      labs(title = "Figure : Gender inequality index",caption = "Source : World Bank", x=NULL, y=NULL, color =NULL, fill=NULL)+
      geom_abline(intercept = 0, slope = 0, color="white")+
      scale_x_continuous(breaks=seq(from=1990, to = 2020, by=5))+
      scale_y_continuous(breaks = seq(0, 0.8, by =0.2),
                         labels=c("",seq(0.2,0.8, by=0.2)))+ 
       ylim(0,0.8)+
       theme_minimal()+
       theme(
         plot.title = element_text(face = "bold", size = 10,"italic", colour = "#85C1E9"),
         panel.grid.minor = element_blank(),
         panel.grid.major.y = element_line(linetype = "dashed")
         )
ggplotly(p, tooltip = "text")%>%
      plotly::layout(legend=list(x=0, #Paramètre pour la légende
                                y = 1.04,
                                 orientation='h'), annotations = 
                       #Paramètre pour la source
 list(x = 0.2, y = -0.08, text = "Source: UNDP", 
      showarrow = F, xref='paper', yref='paper', 
      xanchor='right', yanchor='auto', xshift=0, yshift=0,
      font=list(size=12, color="black")))